package com.ciei.dpagm.service.conditionRule.factMode;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.enums.*;
import com.ciei.dpagm.entity.*;
import com.ciei.dpagm.service.*;
import com.ciei.dpagm.service.conditionRule.ConditionHandleService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.ApplicationContextHolder;
import com.ciei.dpagm.util.JsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pp
 * @date 2023/03/14
 */
public abstract class AbstractFactModeService {

    private ParameterModelService parameterModelService;

    private ParameterModuleService parameterModuleService;

    private ParameterService parameterService;

    protected static Integer oneFactModeIndex = 0;
    protected static Integer twoFactModeIndex = 1;
    protected static Integer threeFactModeIndex = 2;
    protected static Integer modelIdIndex = 2;
    protected static Integer moduleIdIndex = 3;
    protected static Integer valueIdIndex = 4;
    protected static Integer parameterValueIndex = 4;

    /**
     * 解析
     * @param factModeList 事实方式列表
     * @param originData 源数据
     * @param twoFactModeEnum 第二级事实方式
     * @return
     */
    protected abstract JSONObject analysis(List<String> factModeList, JSONObject originData,FactModeEnum twoFactModeEnum);

    /**
     * 转换下拉框列表
     * @param factModeList 事实方式列表
     * @param sonFactModeOptionDataList 子事实方式下拉框数据列表
     * @param twoFactModeEnum 第二级事实方式枚举
     */
    protected abstract void conversionOptionList(List<String> factModeList,List<JSONObject> sonFactModeOptionDataList,FactModeEnum twoFactModeEnum);



    /**
     * 处理变量下拉框列表
     * @param factModeList 事实方式列表
     * @param sonFactModeOptionDataList 子事实方式数据列表
     * @param isHandleParameter 是否处理变量(变量模型、变量模块、变量(fasle表示不处理此下拉框))
     */
    public void conversionParameterOptionList(List<String> factModeList,List<JSONObject> sonFactModeOptionDataList,Boolean isHandleParameter){
        parameterModelService = ApplicationContextHolder.getBean(ParameterModelService.class);
        parameterModuleService = ApplicationContextHolder.getBean(ParameterModuleService.class);
        List<JSONObject> parameterOptionDataList = new ArrayList<>();
        if(isHandleParameter) {
            parameterService = ApplicationContextHolder.getBean(ParameterService.class);
            if (factModeList.get(valueIdIndex) != null) {
                List<String> valueList = factModeList.subList(valueIdIndex, factModeList.size());
                List<Parameter> parameterList = parameterService.findListByIds(valueList);
                for (Parameter parameter : parameterList) {
                    JSONObject parameterOptionData = new JSONObject();
                    parameterOptionData.fluentPut("value", RuleSetService.valueIndex + RuleSetService.optionValueSeparator + parameter.getParameterId())
                            .fluentPut("label", parameter.getParameterName());
                    parameterOptionDataList.add(parameterOptionData);
                }
            }
        }
        List<JSONObject> parameterModuleOptionDataList = new ArrayList<>();
        if(factModeList.get(moduleIdIndex) != null){
            JSONObject parameterModuleData = new JSONObject();
            String parameterModuleId = factModeList.get(moduleIdIndex);
            ParameterModule parameterModule = parameterModuleService.findById(factModeList.get(moduleIdIndex));
            parameterModuleData.fluentPut("value",RuleSetService.moduleIndex + RuleSetService.optionValueSeparator + Integer.valueOf(parameterModuleId))
                    .fluentPut("label",parameterModule == null || parameterModule.getPropertyName() == null ? "---" : parameterModule.getPropertyName());
            if(isHandleParameter){
                parameterModuleData.fluentPut("children",parameterOptionDataList);
            }
            parameterModuleOptionDataList.add(parameterModuleData);
        }
        if(factModeList.get(modelIdIndex) != null){
            JSONObject parameterModelData = new JSONObject();
            String parameterModelId = factModeList.get(modelIdIndex);
            ParameterModel parameterModel = parameterModelService.findById(factModeList.get(modelIdIndex));
            parameterModelData.fluentPut("value",RuleSetService.modelIndex + RuleSetService.optionValueSeparator + Integer.valueOf(parameterModelId))
                    .fluentPut("label",parameterModel == null || parameterModel.getPropertyName() == null ? "---" : parameterModel.getPropertyName())
                    .fluentPut("children",parameterModuleOptionDataList);
            sonFactModeOptionDataList.add(parameterModelData);
        }
    }



}
